package com.mes.web.controller.zc;

import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mes.entity.cb.Materiel;
import com.mes.entity.hy.Product;
import com.mes.entity.lz.ProductUnionMaterial;
import com.mes.entity.lz.QYT_MaterielPage;
import com.mes.entity.lz.QYT_ProductPage;
import com.mes.entity.yzl.Condition;
import com.mes.entity.yzl.CustomerOrder;
import com.mes.entity.yzl.ListBack;
import com.mes.entity.zc.MaterNum;
import com.mes.service.zc.ZcBomService;
import com.mes.utils.ResponseUtil;
import com.mes.utils.StringUtil;
import com.mes.web.controller.yzl.Sup;
/**
 * 
 * @ClassName: ZcBomController 
 * @Description:BOM管理
 * @Author zc
 * @Date 2018年3月30日 上午10:33:09 
 * @version 1.0
 */
@Controller
@RequestMapping("/zcBom")
public class ZcBomController {
	public ProductUnionMaterial productUnionMaterial =null;
	@Resource
	ZcBomService bomService;
	/**
	 * 查询数据库中的所有产品实体
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/pageQueryProductAll")
	public void pageQueryProductAll(HttpServletResponse response, HttpServletRequest request, QYT_ProductPage qyt_ProductPage, String pNum) throws Exception{
		String page = request.getParameter("offset");
		String pageSize = request.getParameter("limit");
		/*page且pageSize都不为空*/
		if (StringUtil.isNotEmpty(page)&&StringUtil.isNotEmpty(pageSize)) {
			qyt_ProductPage.setPage((Integer.parseInt(page))/(Integer.parseInt(pageSize)) + 1);
			qyt_ProductPage.setPageSize(Integer.parseInt(pageSize));
		}
		/*根据产品编号查询*/
		if (StringUtil.isNotEmpty(pNum)) {
			qyt_ProductPage.setNum(new String(pNum .getBytes("utf-8"),"utf-8"));
		}else{
			qyt_ProductPage.setNum(null);
		}
		List<Product> productList = bomService.pageQueryProductAll(qyt_ProductPage);
		if (productList.size() >= 0) {
			int productCount = bomService.pageQueryProductAllCount(qyt_ProductPage);
			JSONArray jsonArray = JSONArray.fromObject(productList);
			JSONObject config = new JSONObject();
			config.put("rows", jsonArray);
			config.put("total", productCount);
			ResponseUtil.write(response, config);
		}
	}
	
	/**
	 * 查询数据库中所有物料实体
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/pageQueryMaterieAll")
	public void pageQueryMaterieAll(HttpServletResponse response, HttpServletRequest request, QYT_MaterielPage qyt_MaterielPage, String mNum) throws Exception{
		String page = request.getParameter("offset");
		String pageSize = request.getParameter("limit");
		/*page且pageSize都不为空*/
		if (StringUtil.isNotEmpty(page)&&StringUtil.isNotEmpty(pageSize)) {
			qyt_MaterielPage.setPage((Integer.parseInt(page))/(Integer.parseInt(pageSize)) + 1);
			qyt_MaterielPage.setPageSize(Integer.parseInt(pageSize));
		}
		/*根据物料编号查询*/
		if (StringUtil.isNotEmpty(mNum)) {
			qyt_MaterielPage.setNumber(new String(mNum .getBytes("utf-8"),"utf-8"));
		}else{
			qyt_MaterielPage.setNumber(null);
		}
		List<Materiel> materielList = bomService.pageQueryMaterieAll(qyt_MaterielPage);
		if (materielList.size() >= 0) {
			int materielCount = bomService.pageQueryMaterieAllCount(qyt_MaterielPage);
			JSONArray jsonArray = JSONArray.fromObject(materielList);
			JSONObject config = new JSONObject();
			config.put("rows", jsonArray);
			config.put("total", materielCount);
			ResponseUtil.write(response, config);
		}
	}
	
	/**
	 * 收集到客户设置的信息保存到数据库
	 * @param idsssArray js传过来的数组
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/receive2TabAndSave")
	public void receive2TabAndSave(@RequestParam(value = "idsArray[]") String[] idssArray, @RequestParam(value = "numArray[]") String[] numArrays, 
									HttpServletResponse response, 
									HttpServletRequest request) throws Exception{
		
		String pNum1 = null;
		MaterNum materNum = new MaterNum();
		if (idssArray.length > 0) {
			productUnionMaterial = new ProductUnionMaterial();
			// 获取第一个选择的产品的编号
			String pNum = request.getParameter("pNum1");
			if(pNum.equals(new String(pNum.getBytes("iso8859-1"), "iso8859-1"))) {
				
				pNum1 = new String(pNum .getBytes("iso8859-1"),"utf-8");
			}else {
				pNum1 = pNum;
			}
			System.out.println("勾选中的产品编号为：" + pNum1);
			if (StringUtil.isNotEmpty(pNum1)) {
				productUnionMaterial.setProductNum(pNum1);
				materNum.setProductid(pNum1);
			}else{
				productUnionMaterial.setProductNum(null);
				materNum.setProductid(null);
			}
			// 先去查数据库中是否存在该实体
			List<ProductUnionMaterial> list = bomService.queryPUMExist(pNum1);
			// 如果没有查到
			if (list.size() > 0) {
				bomService.queryPUMAndSetNull(pNum1);
				StringBuffer sb1 = new StringBuffer();
				// 获取第二个选择的物料的名称(多个)
				for (String string1 : idssArray) {
					sb1.append(string1.concat(","));
					productUnionMaterial.setMaterieNum(sb1.toString());
					System.out.println("A修改操作的数据："+productUnionMaterial.getMaterieNum());
					materNum.setMaterid(productUnionMaterial.getMaterieNum());
				}
				for (String String2 : numArrays) {
					if(StringUtil.isNotEmpty(String2)){
						materNum.setMaternum(Integer.parseInt(String2));
					}
				}
				String materids[] = materNum.getMaterid().split(",");
				for (int i = 0; i < materids.length; i++) {
					materNum.setMaternum(Integer.parseInt(numArrays[i]));
					materNum.setMaterid(materids[i]);
					bomService.insertMaterNum(materNum);
				}
				// 如果数据库存在，就修改该实体
				bomService.receive2TabAndUpdate(productUnionMaterial);
				// 如果查到了
			}else{
				StringBuffer sb2 = new StringBuffer();
				// 获取第二个选择的物料的名称(多个)
				for (String string2 : idssArray) {
					sb2.append(string2.concat(","));
					productUnionMaterial.setMaterieNum(sb2.toString());
					System.out.println("B添加操作的数据："+productUnionMaterial.getMaterieNum());
					materNum.setMaterid(productUnionMaterial.getMaterieNum());
				}
				String materids[] = materNum.getMaterid().split(",");
				for (int i = 0; i < materids.length; i++) {
					materNum.setMaternum(Integer.parseInt(numArrays[i]));
					materNum.setMaterid(materids[i]);
					bomService.insertMaterNum(materNum);
				}
				// 如果数据库不存在，就添加该实体
				bomService.receive2TabAndSave(productUnionMaterial);
			}
			bomService.updateProduct(pNum1);
			response.getWriter().print(1);
		}else{
			response.getWriter().print(0);
		}
	}
	
	@RequestMapping("/findQueryMaterieByPid")
    public @ResponseBody ListBack findQueryMaterieByPid(Condition condition) throws UnsupportedEncodingException{
        ListBack listBack = new ListBack();
        List<Materiel> materielList = new ArrayList<>();
        if(condition.getpNum().equals(new String(condition.getpNum().getBytes("iso8859-1"), "iso8859-1"))) {
        	condition.setpNum(new String(condition.getpNum().getBytes("iso8859-1"),"utf-8"));
		}
        int count = bomService.findQueryMaterieXQCount(condition);
        listBack.setTotal(count);
        materielList = bomService.findQueryMaterieXQ(condition);
        listBack.setRows(materielList);
        return listBack;
    }
}
